home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MuManual / C_Sources / MuLockLib.c < prev    next >
C/C++ Source or Header  |  1999-10-31  |  4KB  |  163 lines

  1. /*************************************************
  2.  ** MuLockLib                                   **
  3.  **                                             **
  4.  ** Load the mmu.library and lock it in memory  **
  5.  **                                             **
  6.  ** Release 40.3, © 1999 THOR Software          **
  7.  ** Gunther Nikl, Thomas Richter, 7.8.1999      **
  8.  *************************************************/
  9.  
  10. /// Include
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <exec/memory.h>
  14.  
  15. #include <dos/dosextens.h>
  16. #include <dos/dostags.h>
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20.  
  21. #include <strings.h>
  22. ///
  23. /// Defines
  24. #define STACKSIZ 512
  25. #define TASKNAME "« MuLockLib »"
  26. ///
  27. /// Structures
  28. struct StartupMsg {
  29.   APTR        su_SysBase;
  30.   struct Task su_Task;
  31.   char        su_TaskName[16];
  32.   char        su_LibName[16];
  33.   ULONG       su_Code[1];
  34. };
  35. //
  36. //
  37. #if defined(__GNUC__)
  38. int Start(void) { return Main(); }
  39. #endif
  40. ///
  41. /// statics
  42. const char version[] = "$VER: MuLockLib 40.3 (7.8.99) © Gunther Nikl, THOR";
  43. ///
  44. /// Protos
  45. static struct StartupMsg *BuildStartup(APTR SysBase);
  46. static void __stdargs RunBack(struct StartupMsg *su);
  47. static void EndTag(void);
  48. int __saveds Main(void);
  49. ///
  50. /// main
  51. int __saveds Main(void)
  52. {
  53. struct ExecBase *SysBase;
  54. struct DosLibrary *DOSBase;
  55. struct Process *proc;
  56. struct Message *msg;
  57. struct Task *lt;
  58. int rc = 20;
  59.  
  60.         SysBase = *((struct ExecBase **)(4L));
  61.         proc = (struct Process *)FindTask(NULL);
  62.         msg = NULL;
  63.  
  64.         if (proc->pr_CLI==NULL) {
  65.                 WaitPort(&proc->pr_MsgPort);
  66.                 msg=GetMsg(&proc->pr_MsgPort);
  67.         }
  68.  
  69.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  70.                 Forbid();
  71.                 if (lt=FindTask(TASKNAME))
  72.                         Signal(lt,SIGBREAKF_CTRL_C);
  73.                 Permit();
  74.  
  75.                 if (lt) {
  76.                         if (!msg) Printf("MuLockLib removed.\n");
  77.                         rc = 0;
  78.                 } else if (!BuildStartup(SysBase)) {
  79.                         if (!msg) Printf("MuLockLib: Failed to launch the background process.\n");
  80.                 } else rc = 0;
  81.  
  82.                 CloseLibrary((struct Library *)DOSBase);
  83.         }
  84.  
  85.         if (msg) {
  86.                 Forbid();
  87.                 ReplyMsg(msg);
  88.         }
  89.  
  90.         return rc;
  91. }
  92. ///
  93. /// BuildStartup
  94.  
  95. struct newMemList {
  96.   struct Node     nml_Node;
  97.   UWORD           nml_NumEntries;
  98.   struct MemEntry nml_ME[2];
  99. };
  100.  
  101. static struct StartupMsg *BuildStartup(APTR SysBase)
  102. {
  103. struct StartupMsg *su;
  104. struct newMemList nml;
  105. struct MemList *ml;
  106. ULONG codesize;
  107. char *p;
  108.  
  109.         codesize                = (ULONG)(EndTag)-(ULONG)(RunBack);
  110.         nml.nml_NumEntries      = 2;
  111.         nml.nml_ME[0].me_Reqs   = MEMF_PUBLIC|MEMF_CLEAR;
  112.         nml.nml_ME[0].me_Length = sizeof(struct StartupMsg)+codesize;
  113.         nml.nml_ME[1].me_Reqs   = MEMF_PUBLIC;
  114.         nml.nml_ME[1].me_Length = STACKSIZ;
  115.  
  116.         ml = AllocEntry((struct MemList *)&nml);
  117.  
  118.         if (!((unsigned int)ml&(1<<31))) {
  119.                 su = ml->ml_ME[0].me_Addr;
  120.                 p  = (char *)ml->ml_ME[1].me_Addr;
  121.  
  122.                 su->su_SysBase = SysBase;
  123.                 su->su_Task.tc_Node.ln_Type = NT_TASK;
  124.                 su->su_Task.tc_Node.ln_Pri  = -1;
  125.                 su->su_Task.tc_Node.ln_Name = su->su_TaskName;
  126.                 su->su_Task.tc_SPLower      = p;
  127.                 su->su_Task.tc_SPReg        = (p+=STACKSIZ-4);
  128.                 ((APTR *)p)[0] = su;        /* push the function argument already to the stack */
  129.                 su->su_Task.tc_SPUpper      = (p+=sizeof(APTR));
  130.                 NewList(&su->su_Task.tc_MemEntry);
  131.                 AddHead(&su->su_Task.tc_MemEntry,&ml->ml_Node);
  132.                 strcpy(su->su_TaskName,TASKNAME);
  133.                 strcpy(su->su_LibName,"mmu.library");
  134.                 CopyMem((ULONG *)(RunBack),su->su_Code,codesize);
  135.                 CacheClearU();
  136.                 if (!AddTask(&su->su_Task,su->su_Code,0)) {
  137.                         FreeEntry(ml);
  138.                         su = NULL;
  139.                 }
  140.         } else su = NULL;
  141.  
  142.   return su;
  143. }
  144.  
  145. ///
  146. /// RunBack
  147. static void __stdargs RunBack(struct StartupMsg *su)
  148. {
  149. APTR SysBase = su->su_SysBase;
  150. struct Library *mulib;
  151.  
  152.         if (mulib=OpenLibrary(su->su_LibName,0)) {
  153.                 Wait(SIGBREAKF_CTRL_C);
  154.                 CloseLibrary(mulib);
  155.         }
  156. }
  157.  
  158. static void EndTag(void)
  159. {
  160. }
  161.  
  162. ///
  163.